package com.hermit.qmst.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.mode.MessageInfo;
import com.hermit.qmst.tools.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/5 0005.
 */
public class MessageAdapter extends BaseAdapter {
    private Context mContext;
    private String TAG = "MessageAdapter";
    public static List<MessageInfo> data;

    public MessageAdapter(Context context) {
        this.mContext = context;
        data = new ArrayList<MessageInfo>();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
//        return 10;
        return data.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hodler hodler = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_message, null);
            hodler = new Hodler();
//            hodler.ll_item = (LinearLayout) convertView.findViewById(R.id.ll_item);
            hodler.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            hodler.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            hodler.tv_year = (TextView) convertView.findViewById(R.id.tv_year);
            hodler.tv_day = (TextView) convertView.findViewById(R.id.tv_day);

            hodler.ll_right = (LinearLayout) convertView.findViewById(R.id.ll_right);
            convertView.setTag(hodler);
        } else {
            hodler = (Hodler) convertView.getTag();
        }

//        GradientDrawable itemDrawable =(GradientDrawable) hodler.ll_item.getBackground();
//        itemDrawable.setStroke(1,Color.parseColor(bgcolor(position)));

        GradientDrawable drawable = (GradientDrawable) hodler.ll_right.getBackground();
        drawable.setColor(Color.parseColor(bgcolor(position)));

        hodler.tv_title.setText(data.get(position).getTitle());

        try {
            hodler.tv_content.setText(Utils.delHTMLTag(Html.fromHtml(data.get(position).getContent()).toString()));
        } catch (Exception e) {
            hodler.tv_content.setText(data.get(position).getTitle());
        }
        String createTime = data.get(position).getCreatetime();
        hodler.tv_day.setText(createTime.substring(8, 10));
        hodler.tv_year.setText(createTime.substring(0, 7));
        return convertView;
    }


    private class Hodler {
//        private LinearLayout ll_item;
        private LinearLayout ll_right;
        private TextView tv_day;
        private TextView tv_year;
        private TextView tv_title;
        private TextView tv_content;

    }

    private String bgcolor(int position) {
        String color = "";
        int i = position % 5;
        switch (i) {
            case 0:
                color = "#0ABE8C";
                break;
            case 1:
                color = "#E5CC16";
                break;
            case 2:
                color = "#FF5E19";
                break;
            case 3:
                color = "#F23B19";
                break;
            case 4:
                color = "#09ADC0";
                break;
        }
        return color;
    }
}
